function countDown(endtime){
	var now = new Date();
	var leftTime = endtime * 1000 - now.getTime();
	var str = '';
	if (leftTime > 0) {
		var dd = parseInt(leftTime / 1000 / 60 / 60 / 24, 10);
		var hh = parseInt(leftTime / 1000 / 60 / 60 % 24, 10);
		var mm = parseInt(leftTime / 1000 / 60 % 60, 10);
		var ss = parseInt(leftTime / 1000 % 60, 10);
		if (dd) str += '<em>'+dd+'</em>天';
		if (dd || hh) str += '<em>'+hh+'</em>时';
		if (dd || hh || mm) str += '<em>'+mm+'</em>分';
		if (dd || hh || mm || ss) str += '<em>'+ss+'</em>秒';
	}
	return str;
}

function isNumber(val) {
	var regPos = /^\d+(\.\d+)?$/; //非负浮点数
	var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
	if (regPos.test(val) || regNeg.test(val)) {
		return true;
	}else{
		return false;
	}
}

// 退出登录
function logout() {
	layer.open({type:2});
	$.get('/public/logout').then(function(data){
		$.msg(data);
	}, function(){
		$.msg({msg:'通讯错误'});
	});
}

$.msg = function(data){
	layer.closeAll();
	var time = typeof(data.time) != 'undefined' ? data.time : 1;
	var msg = {content:data.msg, skin:'msg', time:time};
	if (typeof(data.url) != 'undefined') msg['end'] = function(){
		window.location.href = data.url;
	}
	if (typeof(data.reload) != 'undefined') msg['end'] = function(){
		window.location.reload();
	}
	layer.open(msg);
}

$.notice = function(title, content){
	layer.open({
		title: [title, 'background-color:#eb5852;color:#fff'],
		content: content,
		btn: '我知道了'
	});	
}

$(function(){
	// 禁止提交空表单
	$('form:not(.enable) input').keyup(function(){
		var disabled = false;
		var button = $("form button[type='submit']");
		$('form input').each(function(){
			if ($(this).val() == '') {
				disabled = true;
				return;
			}
		});
		disabled ? button.attr('disabled', true) : button.removeAttr('disabled');
	});

	// Ajax提交表单
	$('form.form, .login form').submit(function(){
		layer.open({type:2});
		$.post(window.location.href, $(this).serialize()).then(function(data){
			$.msg(data);
		}, function(){
			$.msg({msg:'通讯错误'});
		});
		return false;
	});

	$('.hxd input').not('.on').focus(function(){
		$(this).prev().addClass('on');
	});
	$('.hxd input').blur(function(){
		if ($(this).val() == '') $(this).prev().removeClass('on');
	});
	$('.hxd .pwd .iconfont').click(function(){
		var input = $(this).prev('input');
		if (input.attr('type') == 'password') {
			input.attr('type', 'text');
			$(this).removeClass('icon-pwd-hide').addClass('icon-pwd-show');
		} else {
			input.attr('type', 'password');
			$(this).removeClass('icon-pwd-show').addClass('icon-pwd-hide');
		}
	});
	$('.hxd form').submit(function(){
		layer.open({type:2});
		$.post(window.location.href, $(this).serialize()).then(function(data){
			$.msg(data);
			if (data.status == 0) $('.vcode img').attr('src', '/user/vcode?'+Math.random());
		}, function(){
			$.msg({msg:'通讯错误'});
		});
		return false;
	});

	$('#pay a.button').click(function(){
		layer.open({type:2});
		$.post(window.location.href).then(function(data){
			if (data.status == 2) {
				layer.closeAll();
				layer.open({
					type: 1,
					content: '<img style="width:90%;padding:0 5%;" src="/themes/red/assets/img/browser.png">',
					style: 'position:fixed;top:0;left:0;width:100%;height:100%;border:none;background:rgba(0,0,0,.5);'
				});
				return false;
			}
			$('.pay-form').html(data.form);
			$.msg(data);
			if (data.status == 1) {
				setTimeout(function(){
					$('#pay .pay-form form').submit();
				}, 1000);
			}
		}, function(){
			$.msg({msg:'通讯错误'});
		});
		return false;
	});

	$('#pay').on('click', '.layui-m-layerchild', function(){
		layer.closeAll();
	});

	$('#user-cash input').keyup(function(){
		var money = $(this).val();
		if (!$.isNumeric(money)) {
			$(this).val('');
		}
	});
	$('#user-cash a.button').click(function(){
		var money = $('#user-cash input[name="money"]').val();
		if (!$.isNumeric(money)) {
			$.msg({msg:'提现金额输入错误'});
		} else {
			if (money > used) $.msg({msg:'账户余额不足'});
			if (money < cash_min) $.msg({msg:'提现金额不得小于'+cash_min});
			if (cash_max > 0 && money > cash_max) $.msg({msg:'提现金额不得大于'+cash_max});
			layer.open({type:2});
			$.post('/user/cash_apply', {money: money}).then(function(data){
				$.msg(data);
			}, function(){
				$.msg({msg:'通讯错误', reload:1});
			});
		}
		return false;
	});
	$('#user-cash .list').on('click', 'a.cancel', function(){
		layer.open({type:2});
		$.post('/user/cash_cancel', {id:$(this).data('id')}).then(function(data){
			$.msg(data);
		}, function(){
			$.msg({msg:'通讯错误'});
		});
		return false;
	});

	// 无限加载
	if ($('body.dropload').length) {
		var page = 0;
		$('body').dropload({
			scrollArea: window,
			loadDownFn: function(e){
				page++;
				$.post(window.location.href, {page:page}).then(function(data){
					$('.list').append(data.html);
					if (data.status == 1) {
						e.lock();
						e.noData();
						if (page == 1 && data.html == '') $('.list').after('<div class="empty"><img src="/themes/red/assets/img/empty.png"></div>');
					}
					e.resetload();
				}, function(){
					$.msg({msg:'通讯错误'});
					e.resetload();
				});
			}
		});
	}
});